#include  <cstdio>

inline int min(int a, int b)
{
	return a < b ? a: b;
}

int res[10000001];

int main(int argc, char *argv[])
{
	res[1] = 1;
	int i2 = 1, i3 = 1;
	for( int i = 2; i <= 10000000 ; ++i )
	{
		res[i] = min(res[i2]*3 + 1, res[i3]*2 +1);
		if(res[i] == res[i2]*3 +1)
			i2++;
		if(res[i] == res[i3]*2 +1)
			i3++;
	}
	
	int n;
	while(scanf("%d",&n) != EOF)
		printf("%d\n",res[n]);
}
